package com.MRSyc.config;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * V1.0
 */
public class GeneratorServiceEntity {
	private static String projectPath = System.getProperty("user.dir");
    //锟斤拷锟斤拷路锟斤拷
    private static String parentPackageName = "com.MRSyc";
    //锟斤拷锟斤拷锟斤拷锟斤拷
    private static String authorName = "Fxq";
 
    /**
     * 全锟斤拷锟斤拷锟斤拷
     *
     * @return
     */
    public static GlobalConfig globalConfig() {
 
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(projectPath + "/src/main/java")
                // 锟角凤拷支锟斤拷 AR
                .setActiveRecord(true)
                //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
                .setAuthor(authorName)
                //锟侥硷拷锟斤拷锟斤拷(全锟斤拷锟侥硷拷)
                .setFileOverride(true)
                //锟斤拷锟斤拷锟斤拷锟斤拷
                .setIdType(IdType.AUTO)
                //SQL 映锟斤拷锟侥硷拷
                .setBaseResultMap(true)
                //SQL 片锟斤拷
                .setBaseColumnList(true)
                .setSwagger2(false)
                .setEnableCache(false)
                .setOpen(false)
                //时锟斤拷锟斤拷锟斤拷
                .setDateType(DateType.ONLY_DATE)
                .setEnableCache(false)
        ;
        return globalConfig;
    }
 
    /**
     * 锟斤拷锟斤拷源锟斤拷锟斤拷
     *
     * @return
     */
    public static DataSourceConfig dataSourceConfig() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        //ResourceBundle rootResource = ResourceBundle.getBundle("application");
       /* String url = rootResource.getString("spring.datasource.url");
        String username = rootResource.getString("spring.datasource.username");
        String password = rootResource.getString("spring.datasource.password");
        String driverClassName = rootResource.getString("spring.datasource.driverClassName");*/
        String url="jdbc:sqlserver://localhost:1433;DatabaseName=AIS20181212204301";
        String username="sa";
        String password="123456";
        String driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
 
        dataSourceConfig
                .setDbType(DbType.SQL_SERVER)
                .setUrl(url)
                .setDriverName(driverClassName)
                .setUsername(username)
//                .setSchemaName("public")
                .setPassword(password);
        return dataSourceConfig;
    }
 
    /**
     * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
     *
     * @return
     */
    public static PackageConfig packageConfig(String moduleName) {
        PackageConfig packageConfig = new PackageConfig();
        //锟斤拷锟矫革拷锟斤拷路锟斤拷
        packageConfig.setParent(parentPackageName);
               /* .setMapper("mybatis.mapper")
                .setXml("mybatis.mapper")
                .setEntity("mybatis.entity")
                .setService("service")
                //锟斤拷锟皆讹拷锟斤拷锟斤拷 impl锟斤拷锟斤拷锟皆诧拷锟借定
                .setServiceImpl("service.impl")
                .setController("controller");*/
 
        if (StringUtils.isEmpty(moduleName)) {
            //锟斤拷锟斤拷业锟斤拷锟铰凤拷锟�
            packageConfig.setModuleName(moduleName);
        }
 
        return packageConfig;
    }
 
    /**
     * 锟斤拷锟斤拷模锟斤拷
     *
     * @return
     */
    public static InjectionConfig injectionConfig(String moduleName) {
        InjectionConfig injectionConfig = new InjectionConfig() {
            //锟皆讹拷锟斤拷锟斤拷锟斤拷注锟斤拷:abc
            //锟斤拷.ftl(锟斤拷锟斤拷锟斤拷.vm)模锟斤拷锟叫ｏ拷通锟斤拷${cfg.abc}锟斤拷取锟斤拷锟斤拷
            @Override
            public void initMap() {
//                Map<String, Object> map = new HashMap<>();
//                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
//                this.setMap(map);
            }
        };
 
        // 锟皆讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
        List<FileOutConfig> focList = new ArrayList<>();
        // 锟斤拷锟侥ｏ拷锟斤拷锟斤拷锟斤拷锟� freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 锟斤拷锟侥ｏ拷锟斤拷锟斤拷锟斤拷锟� velocity
        // String templatePath = "/templates/mapper.xml.vm";
 
        // 锟皆讹拷锟斤拷锟斤拷锟矫会被锟斤拷锟斤拷锟斤拷锟�
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
            	
                if (StringUtils.isEmpty(moduleName)) {
                    // 锟皆讹拷锟斤拷锟斤拷锟斤拷募锟斤拷锟� 锟斤拷 锟斤拷锟斤拷锟� Entity 锟斤拷锟斤拷锟斤拷前锟斤拷缀锟斤拷锟剿达拷注锟斤拷 xml 锟斤拷锟斤拷锟狡伙拷锟斤拷欧锟斤拷锟斤拷浠拷锟斤拷锟�
                    return projectPath + "/src\\main\\resources\\xml\\mapper/"
                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                } else {
                    return projectPath + "/src\\main\\resources\\xml\\mapper/"
                            + moduleName + "/"
                            + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                }
 
            }
        });
        injectionConfig.setFileOutConfigList(focList);
 
        return injectionConfig;
    }
 
    /**
     * 锟斤拷锟缴诧拷锟斤拷锟斤拷锟斤拷
     *
     * @param tableName
     * @return
     */
    public static StrategyConfig strategyConfig(String moduleName, String... tableName) {
        StrategyConfig strategyConfig = new StrategyConfig();
        //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷  underline_to_camel 锟斤拷锟竭憋拷锟秸凤拷
        strategyConfig.setNaming(NamingStrategy.underline_to_camel)
                //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷  underline_to_camel 锟斤拷锟竭憋拷锟秸凤拷
                .setColumnNaming(NamingStrategy.underline_to_camel)
                //锟斤拷锟矫继筹拷锟斤拷
                //.setSuperEntityClass("com.maoxs.pojo")
                //锟斤拷锟矫继筹拷锟斤拷
                //.setSuperControllerClass("com.maoxs.controller")
                //锟角凤拷锟斤拷锟絣ombok
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                .setControllerMappingHyphenStyle(true)
                //锟斤拷锟矫憋拷锟斤拷
                .setInclude(tableName)
                //锟斤拷锟矫筹拷锟斤拷锟斤拷
//                .setSuperEntityColumns("id")
                //锟斤拷锟斤拷controller映锟斤拷锟斤拷锟街凤拷
                .setControllerMappingHyphenStyle(true)
                //锟斤拷锟角白�
                .setTablePrefix(packageConfig(moduleName).getModuleName() + "_");
        return strategyConfig;
    }
 
    /**
     * 锟斤拷锟斤拷模锟斤拷
     *
     * @return
     */
    public static TemplateConfig templateConfig() {
        // 锟斤拷锟斤拷模锟斤拷
        TemplateConfig templateConfig = new TemplateConfig();
 
        // 锟斤拷锟斤拷锟皆讹拷锟斤拷锟斤拷锟侥ｏ拷锟�
        //指锟斤拷锟皆讹拷锟斤拷模锟斤拷路锟斤拷锟斤拷注锟解不要锟斤拷锟斤拷.ftl/.vm, 锟斤拷锟斤拷锟绞癸拷玫锟侥ｏ拷锟斤拷锟斤拷锟斤拷远锟绞讹拷锟�
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        templateConfig.setController("/templates/controller.java");
 
        templateConfig.setXml(null);
        return templateConfig;
    }
 
    public static void Generator(String moduleName, String... tableName) {
        AutoGenerator mpg = new AutoGenerator()
                .setCfg(injectionConfig(moduleName))
                .setTemplate(templateConfig())
                .setGlobalConfig(globalConfig())
                .setDataSource(dataSourceConfig())
                .setPackageInfo(packageConfig(moduleName))
                .setStrategy(strategyConfig(moduleName, tableName))
//              选锟斤拷 freemarker 锟斤拷锟斤拷锟斤拷要指锟斤拷锟斤拷锟铰加ｏ拷注锟斤拷 pom 锟斤拷锟斤拷锟斤拷锟斤拷锟叫ｏ拷默锟斤拷 Veloctiy
                .setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
 
    public static void main(String[] args) {
        Generator(null, new String[]{"T_OUT_RESULT"});
    }
}
